
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "geometry.h"
#include "polygon.h"
#include "sweep.h"
#include "voronoi.h"

#include "../common.h"


extern int           numBackVoronoi;
extern storedVoronoi *backVorPoints;


void
endProgram(Pgon **polyPtrs, int numPoly)
{
	int	i;

	endVoronoi();

	for (i = 0; i < numPoly; i++)
		killPolygon (&(polyPtrs[i]));
	free (polyPtrs);	

	endPointStore();
}

void
getVoronoi(CELL *headCell, CELL *tailCell, 
           voronoiType **vorList, faceType **faceList, int *howManyVert)
{
  int  howManySites = 0;
  int  count = 0;
  Pt   pt;
  CELL *runner;
  int  i, j;
  Pgon **polyPtrs;
#ifdef saveOutput
  FILE *outfile;
#endif

  useFast = True;

  /*print_list(headCell,tailCell);*/
  /* Find out how many sites I have. */

  runner = headCell->next;
  while (runner != tailCell) {
    howManySites += 1;
    runner = runner->next;
  }

  /* Convert all the sites into polygons. */
  
 if((polyPtrs = (Pgon **) malloc (sizeof(Pgon *) * howManySites))==NULL){
  	printf("Problem with malloc in marcelo.c!\n");
    exit(0);
 }
#ifdef saveOutput
  /*outfile = fopen("list.of.points", "w");*/
#endif

  runner = headCell->next; 
  while (runner != tailCell) {
    pt.w = 1.0;
    pt.x = runner->x;
    pt.y = runner->y;
    polyPtrs[count] = newPolygon(1, &pt);
    polyPtrs[count]->origin = (Ptr) runner;
    /*fprintf(outfile, "%f %f\n", pt.x, pt.y);*/
    count++;
    runner = runner->next;
  }
#ifdef saveOutput
  /*fclose(outfile);*/
#endif

  /* Allocate space for the Voronoi code. */

  initVoronoi(howManySites);

  /* Get all my information ahead of time. */

  demoPass = False;
  readyPointStore(12*howManySites+3);
  sweep (polyPtrs, howManySites, 1); 

  /* The whole Voronoi diagram has now been computed.  Translate
     all the information into the appropriate output format. */

  *vorList  = (voronoiType *) malloc( sizeof(voronoiType) * (numBackVoronoi+1) );
  *faceList = (faceType *)    malloc( sizeof(faceType) * howManySites );

  fprintf(stderr, "Computed Voronoi diagram. %d vertices.\n", numBackVoronoi+1);
 
  if ((*vorList != NULL) && (*faceList != NULL)) {
	/* Transfer all the Voronoi points.  The structure for backVorPoints
       	is in the file voronoi.h */

    for (i = 1; i <= numBackVoronoi; i++) {
      (*vorList)[i].x = XVal(backVorPoints[i].voronoi);
      (*vorList)[i].y = YVal(backVorPoints[i].voronoi);
      (*vorList)[i].sites[0] = (CELL *)backVorPoints[i].generator[0]->origin;
      (*vorList)[i].sites[1] = (CELL *)backVorPoints[i].generator[1]->origin;
      (*vorList)[i].sites[2] = (CELL *)backVorPoints[i].generator[2]->origin;
    }

    *howManyVert = numBackVoronoi;

    /* Transfer the face information. */

    for (i = 0; i < howManySites; i++) {
      (*faceList)[i].faceSize = polyPtrs[i]->voronoiCount;

      for (j = 0; j < polyPtrs[i]->voronoiCount; j++)
      {
        (*faceList)[i].vorPts[j] = polyPtrs[i]->voronoiList[j];
      }

      (*faceList)[i].site = (CELL *)polyPtrs[i]->origin;
    }
  } else{
	printf("Problem with malloc in marcelo.c!\n");
	exit(2);
  }
 
  /* Get rid of the space allocated by the Voronoi routines. */

  endProgram(polyPtrs, howManySites);
}

